﻿using Helper;
using HZY.Framework.DependencyInjection;
using Mapster;
using Model;
using Model.Entity;
using Model.EntityInfo;
using SqlSugar;

namespace DAL.Service
{
    public class DataLogService : BaseService<DataLogEntity>, IScopedSelfDependency
    {
        public async Task<BaseResult<ResPageData<DataLogInfoModel>>> GetDataLogListAsync(int page, int pageSize, string StartDate, string EndDate)
        {
            try
            {
                var exp = Expressionable.Create<DataLogEntity>();
                RefAsync<int> total = 0;
                RefAsync<int> totalPage = 0;
                var qb = DB.SqlSugarClient.Queryable<DataLogEntity>();

                if (!String.IsNullOrEmpty(StartDate) && !String.IsNullOrEmpty(EndDate))
                {
                    try
                    {
                        DateTime beginTime = DateTime.Parse(StartDate);
                        DateTime endTime = DateTime.Parse(EndDate);
                        qb = qb.SplitTable(beginTime, endTime);
                    }
                    catch (FormatException ex)
                    {
                        return new BaseResult<ResPageData<DataLogInfoModel>>()
                        {
                            Code = SystemEnums.ResCode.Fail,
                            Data = new ResPageData<DataLogInfoModel>()
                            {
                                List = new List<DataLogInfoModel>(),
                                Total = 0,
                                TotalPage = 0
                            },
                            Msg = "日期格式错误：" + ex.Message
                        };
                    }
                }
                else
                {
                    qb = qb.SplitTable();
                }
                qb = qb.OrderByPropertyName("CreatedAt", OrderByType.Desc);
                var list = await qb.ToPageListAsync(page, pageSize, total, totalPage);
                Console.WriteLine(StartDate);
                Console.WriteLine(EndDate);
                Console.WriteLine(list.Count);
                return new BaseResult<ResPageData<DataLogInfoModel>>()
                {
                    Code = SystemEnums.ResCode.Success,
                    Data = new ResPageData<DataLogInfoModel>()
                    {
                        List = list.ToArray().Adapt<List<DataLogInfoModel>>(),
                        Total = total,
                        TotalPage = totalPage
                    },
                    Msg = ""
                };
            }
            catch (Exception e)
            {
                return new BaseResult<ResPageData<DataLogInfoModel>>()
                {
                    Code = SystemEnums.ResCode.Fail,
                    Data = new ResPageData<DataLogInfoModel>()
                    {
                        List = new List<DataLogInfoModel>(),
                        Total = 0,
                        TotalPage = 0
                    },
                    Msg = e.Message
                };
            }

        }

        public async Task<BaseResult<string>> CreateDataLogAsync(DataLogEntity Log)
        {
            BaseResult<string> result = new BaseResult<string>();

            //雪花ID+表不存在会建表

            try
            {
                long res = await DB.SqlSugarClient.Insertable(Log).SplitTable().ExecuteReturnSnowflakeIdAsync();
                result.Code = SystemEnums.ResCode.Success;
                result.Msg = "新增成功！";
                result.Data = "";
                return result;
            }
            catch (Exception ex)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "新增失败！";
                result.Data = "";
                return result;
            }
        }
    }
}
